import loginApi from '../../api/sys'
import md5 from 'js-md5'
import storage from '../../utils/storage'

export default {
    state: {
        token: storage.getItem('token') || '',
    },
    mutations: {
        saveToken(state, token) {
            state.token = token
            storage.setItem('token', token)
        }
    },
    actions: {
        doLogin({ commit }, loginForm) {
            let { username, password } = loginForm
            return new Promise((resolve, reject) => {
                loginApi.doLogin(username, md5(password)).then((res) => {
                    // console.log(res);
                    commit('saveToken', res.token);
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}
